<template>
  <div id="register">
    <div class="header">
      <el-page-header @back="goBack" content="忘记密码">
      </el-page-header>
    </div>
    <div class="content">
      <h2 class="content_title">忘记密码</h2>
      <!-- 忘记密码表单 -->
      <el-form class="ResetForm"
               :model="resetForm"
               :rules="rules"
               :inline="false"
               ref="resetForm"
               >
        <el-form-item prop="email">
          <el-input placeholder="用户邮箱号"
                    prefix-icon="el-icon-message"
                    v-model="resetForm.email">
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input placeholder="设置密码"
                    prefix-icon="el-icon-unlock"
                    show-password
                    v-model="resetForm.password">
          </el-input>
        </el-form-item>

        <el-form-item prop="captcha">
          <el-input placeholder="验证码"
                    prefix-icon="el-icon-s-comment"
                    style="width: 55%"
                    v-model="resetForm.captcha">
          </el-input>
          <el-button type="primary"
                     size="medium"
                     style="margin-left: 20px;"
                     @click="sendCaptcha(resetForm)">发送验证码</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="primary"
                     size="medium"
                     style="width: 100%"
                     @click="resetSubmit(resetForm)">提 交</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
import { sendEmail, reset } from 'api/login/login.js'
export default {
  data() {
    return{
      resetForm:{
        email: '',
        password: '',
        captcha: ''
      },
      rules: {
        email: [
          { required: true, message: '请输入邮箱账号', trigger: 'blur' },
          {
            min: 3,
            max: 30,
            type:'email',
            message: '请输入正确的邮箱号',
            trigger:[ 'blur','change']
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '设置长度在 3 到 15 个字符的密码',
            trigger:['blur','change']
          }
        ],
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
        ]
      },
    }
  },
  components:{

  },
  methods:{
    goBack() {
      this.$router.push({ name: 'Login' })
    },
    // 发送邮箱验证码
    sendCaptcha(registerForm) {
      const params = {
        email: registerForm.email,
        type: 1
      }
      const email = params.email;
      if (!email) {
        return  this.$message.warning('请输入邮箱账号')
     }
    if (!/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/.test(email)) {
      return this.$message.warning('请输入正确的邮箱格式')
   }
      sendEmail(params).then(res => {

      }).catch(error=>{
        this.$message.error('发送失败，详细查看控制台')
        console.log(error)
      })
    },
    resetSubmit(resetForm) {
      this.$refs.resetForm.validate((valid)=>{
        if(valid){
          reset(resetForm)
        .then(res => {
          const resData = res.data
          console.log(resData);
          if(resData.success) {
            this.$message({
              message: resData.message,
              type: 'success'
            })
            this.$router.push({ name: 'Login' })
          }else{
            this.$message({
              message: resData.message,
              type: 'error'
            })
          }
        })
        .catch(() => {
          this.$message({
            message: '无法连接到服务器',
            type: 'error'
          })
        })
        }
        else{
          return false;
        }
      })
      
    }
  }
}
</script>

<style lang="scss" scoped>
#register{
  .header{
    user-select: none;
    width: 100%;
    margin: 5px 5px;
  }
  .content{
    width: 500px;
    height: 400px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .content_title{
      text-align: center;
    }
    .ResetForm{
      width: 300px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }
}

</style>
